<?php 
/**
 * This file is part of DomFg.
 * 
 * DomFg is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * DomFg is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with DomFg.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * @link http://inditel.ee/
 * @copyright 2007-2009 Inditel Meedia OÜ
 * @author Oliver Leisalu <oliver@inditel.ee>
 */

/**
* @author Oliver Leisalu, Inditel Meedia
* @package DomFg
* @subpackage Grid
*/
class DomFgGridField {
	
	protected $key;
	
	protected $title;
	
	/**
	 * <code>
//Sample handler function
function myHandler( &$row, $i, $customArg) {
	return $row['name']; // if $row is array.
}
//Definition:
array(
	'handler' => 'myHandler',
	'key' => NULL,
	'handlerArgs' => array($customArg)
);

//Sample handler function
function myHandler( $name, &$row, $i, $customArg) {
	return $name;
}
//Definition:
array(
	'handler' => 'myHandler',
	'key' => name,
	'handlerArgs' => array($customArg)
);
	 * </code>
	 *
	 * @var String
	 */
	protected $handler;
	
	protected $handlerArgs = array();
	
	protected $renderer;
	
	protected $colspan = 0;
	
	protected $width;
	
	protected $orderByField;
	
	protected $orderByDirection = 'ASC';
	
	protected $link;
	
	protected $target;
	
	protected $value;
	
	protected $level;
	
	protected $tooltip;
	
	public function key( $value ) {
		$this->key = $value;
		return $this;
	}
	
	public function link( $value ) {
		$this->link = $value;
		return $this;
	}
	
	public function target( $value ) {
		// @TODO Finish target support for grid field
		throw new RuntimeException('Not yet supported!');
		$this->target = $value;
		return $this;
	}
	
	public function value( $value ) {
		$this->value = $value;
		return $this;
	}
	
	public function colspan( $value ) {
		$this->colspan = (int)$value;
		return $this;
	}
	
	public function width( $value ) {
		$this->width = (int)$value;
		return $this;
	}
	
	public function title( $value ) {
		$this->title = $value;
		return $this;
	}
	
	public function level( $level = 'level' ) {
		$this->level = $level;
		return $this;
	}
	
	public function handler( $value ) {
		$this->handler = $value;
		return $this;
	}
	
	public function handlerArgs( array $value ) {
		$this->handlerArgs = $value;
		return $this;
	}
	
	public function renderer( $value ) {
		$this->renderer = $value;
		return $this;
	}
	
	public function orderByField( $value ) {
		$this->orderByField = $value;
		return $this;
	}
	
	public function orderByDirection( $value ) {
		$this->orderByDirection = $value;
		return $this;
	}
	
	public function getValue( &$row, $i ) {
		
		// value
		if( $this->value ) {
			$newValue = $this->value;
		
		} else if( $this->key ) {
			
			if( is_array($this->key) === true ) {
				$newValue = $row;
				$keys = $this->key;
				while( empty($keys) === false ) {
					$key = array_shift($keys);
					if( is_object($newValue) ) {
						$newValue = $newValue->$key;
					} elseif ( is_array($newValue ) ) {
						$newValue = $newValue[$key];
					} else {
						$newValue = '';
						break;
					}
				}
		
			} else if( is_object($row) ) {
				$newValue = $row->{$this->key};
			} elseif ( is_array($row ) ) {
				$newValue = $row[$this->key];
			
			} else {
			
				$newValue = '';
			}
			
			if( $this->handler !== NULL )
				$newValue =  call_user_func_array( $this->handler, array_merge( array($newValue, $row, $i), (array)$this->handlerArgs ) );
		
		} else {
		
			if( $this->handler !== NULL )
				$newValue =  call_user_func_array( $this->handler, array_merge( array($row, $i), (array)$this->handlerArgs ) );
		}
		// ----
		
		
		// level
		if( $this->level) {
			if( is_array($row) ) {
				$level = $row[$this->level];
			} else {
				$level = $row->{$this->level};
			}
			$level = str_repeat('&nbsp;&nbsp;&nbsp;',$level);

		} else
			$level = '';
		// -----
		
		
		if( $this->renderer !== NULL )
			$style = call_user_func( $this->renderer, $parsedValue );
			
		if( $this->link !== NULL ) {
			$id = NULL;
			if( is_object($row) && $row->id ) {
				$id = $row->id;	
			} else if( is_array($row) && isset($row['id']) ) {
				$id = $row['id'];
			} else {
				$id = $i;
			}
			
			if( strstr( $this->link, '[$id]' ) ) {
				$link = str_replace('[$id]', urlencode($id), $this->link);
			} else {
				$link =  $this->link . urlencode($id);
			}
		}
		
		return array( 'value'=> $level !== '' ? $level.$newValue : $newValue, 'style'=>$style, 'link' => $link );
	}
	
	public function __get( $key  ) {
		return $this->$key;
	}

}
?>